//
//  GJWebController.m
//  CYGJ
//
//  Created by Chen Qian on 2019/4/23.
//  Copyright © 2019 Facebook. All rights reserved.
//

#import "GJWebController.h"
#import <WebKit/WebKit.h>
#import <XWKWebViewProgressView.h>
#import <TYSnapshotScroll/WKWebView+TYSnapshot.h>
#import <UIColor+HXExtension.h>

@interface GJWebController ()<WKNavigationDelegate>

@property (nonatomic, strong) WKWebView *myWebView;

@end

@implementation GJWebController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
  
  self.title = @"网页截图";
  
  WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
  config.allowsInlineMediaPlayback = YES;
  config.allowsAirPlayForMediaPlayback = YES;
  
  self.myWebView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config];
  self.myWebView.navigationDelegate = self;
  self.myWebView.allowsBackForwardNavigationGestures = true;
  [self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.webUrl]]];
  [self.view addSubview:self.myWebView];
  
  self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"截图" style:UIBarButtonItemStylePlain target:self action:@selector(toCrop)];
  
  CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
  CGRect rectNav = self.navigationController.navigationBar.frame;
  NSInteger navigationBarHeight = rectStatus.size.height + rectNav.size.height;
  XWKWebViewProgressView *progressView = [[XWKWebViewProgressView alloc] initWithFrame:CGRectMake(0, navigationBarHeight, rectNav.size.width, 2)];
  [self.view addSubview:progressView];
  [progressView setTrackTintColor:[UIColor whiteColor]];
  [progressView setProgressTintColor:[UIColor colorWithRed:22/255.0 green:173/255.0 blue:253/255.0 alpha:1]];
  [progressView setWebView:self.myWebView];
}

-(void)toCrop{
  [self.myWebView screenSnapshotNeedMask:NO addMaskAfterBlock:^{
    
  } finishBlock:^(UIImage *snapShotImage) {
    if (self.toWebScreenDisplay) {
      self.toWebScreenDisplay(snapShotImage);
    }
    [self.navigationController popViewControllerAnimated:YES];
  }];
}

-(void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:animated];
  [self.navigationController setNavigationBarHidden:NO animated:YES];
}

-(void)viewWillDisappear:(BOOL)animated{
  [super viewWillDisappear:animated];
  [self.navigationController setNavigationBarHidden:YES animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error{
  if (error.description) {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:error.description message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
      [self.navigationController popViewControllerAnimated:YES];
    }]];
    [self presentViewController:alertController animated:YES completion:nil];
    alertController = nil;
  }
}

-(void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error{
  if (error.description) {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:error.description message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
      [self.navigationController popViewControllerAnimated:YES];
    }]];
    [self presentViewController:alertController animated:YES completion:nil];
    alertController = nil;
  }
}

@end
